import request from '@/utils/request'

// 用户查询参数
export interface UserQueryParams {
  current?: number
  size?: number
  username?: string
  realName?: string
  phone?: string
  email?: string
  status?: number
  deptId?: number
}

// 创建用户请求
export interface CreateUserRequest {
  username: string
  password: string
  realName: string
  nickname?: string
  email?: string
  phone?: string
  gender?: number
  birthday?: string
  deptId?: number
  position?: string
  intro?: string
  status?: number
  userType?: number
  remark?: string
  roleIds?: number[]
}

// 更新用户请求
export interface UpdateUserRequest {
  username: string
  password?: string
  realName: string
  nickname?: string
  email?: string
  phone?: string
  gender?: number
  birthday?: string
  deptId?: number
  position?: string
  intro?: string
  status?: number
  userType?: number
  remark?: string
  roleIds?: number[]
}

// 用户API
export const userApi = {
  // 分页查询用户
  getUserPage: (params: UserQueryParams) => {
    return request.get('/users/page', { params })
  },

  // 获取用户列表
  getUserList: () => {
    return request.get('/users/list')
  },

  // 获取用户详情
  getUserById: (id: number) => {
    return request.get(`/users/${id}`)
  },

  // 新增用户
  createUser: (data: CreateUserRequest) => {
    return request.post('/users', data)
  },

  // 更新用户
  updateUser: (id: number, data: UpdateUserRequest) => {
    return request.put(`/users/${id}`, data)
  },

  // 删除用户
  deleteUser: (id: number) => {
    return request.delete(`/users/${id}`)
  },

  // 批量删除用户
  batchDeleteUser: (ids: number[]) => {
    return request.delete('/users/batch', { data: { ids } })
  },

  // 启用/禁用用户
  changeStatus: (id: number, status: number) => {
    return request.put(`/users/${id}/status`, { status })
  },

  // 重置密码
  resetPassword: (id: number, newPassword: string) => {
    return request.put(`/users/${id}/reset-password`, { newPassword })
  },

  // 修改密码
  changePassword: (oldPassword: string, newPassword: string) => {
    return request.put('/users/change-password', { oldPassword, newPassword })
  },

  // 分配角色
  assignRoles: (id: number, roleIds: number[]) => {
    return request.put(`/users/${id}/roles`, { roleIds })
  },

  // 获取用户角色
  getUserRoles: (id: number) => {
    return request.get(`/users/${id}/roles`)
  },

  // 更新个人信息
  updateProfile: (data: any) => {
    return request.put('/users/profile', data)
  },

  // 上传头像
  uploadAvatar: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return request.post('/users/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 导入用户
  importUsers: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return request.post('/users/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 导出用户
  exportUsers: (params: UserQueryParams) => {
    return request.get('/users/export', { 
      params,
      responseType: 'blob'
    })
  }
} 